<?php
use app\assets\AjaxAsset;
use app\models\ArticleCategory;
use app\widgets\FileUploadWidget;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\redactor\widgets\Redactor;
use yii\web\JsExpression;
use yii\widgets\ActiveForm;

/**
 * @var $this yii\web\View
 * @var $model app\models\Article
 */

$this->title = '添加/修改资讯';
$this->params['breadcrumbs'][] = '资讯管理';
$this->params['breadcrumbs'][] = $this->title;

AjaxAsset::register($this);
?>
<?php $form = ActiveForm::begin();?>
    <?php echo Html::activeHiddenInput($model, 'id');?>
    <?php echo $form->field($model, 'cid')->dropDownList([''=>'请选择分类'] + ArticleCategory::list4Options());?>
    <?php echo $form->field($model, 'title');?>
    <?php echo $form->field($model, 'gid')->hint('对应商城商品的编号。');?>
    <?php echo $form->field($model, 'goods_url')->hint('点击直接购买时跳转的链接。');?>
    <?php echo $form->field($model, 'cover_img_fid')
        ->hint($model->cover_img_fid > 0 ? Html::img($model->coverImg->getUrl(), ['width'=>100]) : ' ')
        ->widget(FileUploadWidget::className(), ['url'=>Url::to(['/admin/article/upload']), 'callback'=>'uploadCoverImgCallback']);?>
    <script>
        function uploadCoverImgCallback(fid, url) {
            $('#article-cover_img_fid').val(fid);
            $('.field-article-cover_img_fid .hint-block').html('<img src="' + url + '" width="100" />');
        }
    </script>
    <?php echo $form->field($model, 'intro');?>
    <?php echo $form->field($model, 'content')->widget(Redactor::className(), ['clientOptions'=>[
        'lang'=>'zh_cn',
        'minHeight'=>'200px',
        'maxHeight'=>'500px',
        'plugins'=>['fontcolor', 'fontsize', 'fullscreen', 'table', 'video'],
        'imageUpload'=>Url::to(['/admin/article/upload']),
        'uploadImageFields'=>new JsExpression('{' . Yii::$app->request->csrfParam . ':yii.getCsrfToken()}'),
        'imageUploadCallback'=>new JsExpression(
<<<JJSS
function(image, json) {
    if (callback(json)) {
        $(image).attr('src', json['files'][0]['url']).attr('data-fid', json['files'][0]['fid']);
    }
}
JJSS
        ),
        'fileUpload'=>Url::to(['/admin/article/upload']),
        'uploadFileFields'=>new JsExpression('{' . Yii::$app->request->csrfParam . ':yii.getCsrfToken()}'),
        'fileUploadCallback'=>new JsExpression(
<<<JJSS
function(link, json) {
    if (callback(json)) {
        $(link).attr('href', json['files'][0]['url']).attr('data-fid', json['files'][0]['fid']).html(json['files'][0]['url']);
    }
}
JJSS
        ),
    ]]);?>
    <?php echo $form->field($model, 'tags')->hint('多个标签使用空格隔开。');?>
    <?php echo $form->field($model, 'remark');?>
    <div class="form-group">
        <div class="col-lg-offset-1 col-lg-11">
            <button type="button" class="btn btn-default" onclick="window.history.go(-1);"><i class="ace-icon fa fa-arrow-left bigger-110"></i>返回</button>
            <button type="submit" class="btn btn-primary"><i class="ace-icon fa fa-check bigger-110"></i>保存</button>
            <button type="reset" class="btn btn-warning"><i class="ace-icon fa fa-undo bigger-110"></i>重置</button>
        </div>
    </div>
<?php $form->end();?>
